package com.wasu.sdk_ott.http.network.error;


import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import com.wasu.UILApplication;


/* <p>------------------------------------------------------
 * <p>Copyright (C) 2021 wasu company, All rights reserved.
 * <p>------------------------------------------------------
 * <p>
 *
 * <p>
 *
 * @author yingjh
 * @date on  2021/11/2 15:37.
 */
public class ErrorUtils {
    public static final String IPS_SYSTEM_NAME = "新融合门户";
    public static final String HDYSS_SYSTEM_NAME = "互动云搜索";
    public static final String ORDER_MANAGE_SYSTEM_NAME = "订单系统";
    public static final String BUS_DATA_MANAGE_SYSTEM_NAME = "业务数据管理系统";
    public static final String I_ORDER_SYSTEM_NAME = "订购网关";
    public static final String IAD_SYSTEM_NAME = "广告系统";
    public static final String IPEPG_SYSTEM_NAME = "ipepg系统";
    public static final String IPEPG2_SYSTEM_NAME = "IP直播管理系统";
    public static final String RECOMMEND_SYSTEM_NAME = "推荐系统";
    public static final String USER_CENTER_SYSTEM_NAME = "用户中心系统";

    public static Error errorBean(String errorCode, String errorReason) {
        Error bean = new Error();
        bean.setTotalMem("");
        bean.setAvaiMem("");
        bean.setBusinessName("家庭4K-APK");
        bean.setErrorCode(errorCode);
        bean.setErrorReason(errorReason);
        bean.setRomVersion("");
        bean.setStbId("");
        bean.setUserId("");
        bean.setModel("");
        bean.setApkVersion(getVersionName(UILApplication.getInstance().getApplication()));
        bean.setTime("");
        bean.setBusiParams("");
        bean.setUrl("");
        bean.setUrlBody("");
        bean.setServiceName("");
        bean.setResult("");
        return bean;
    }

    public static String getVersionName(Context context) {
        // 获取packagemanager的实例
        PackageManager packageManager = context.getPackageManager();
        // getPackageName()是你当前类的包名，0代表是获取版本信息
        PackageInfo packInfo = null;
        try {
            packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        if (packInfo != null) {
            String version = packInfo.versionName;
            return version;
        } else {
            return null;
        }
    }

    /**
     * 根据url得到serviceName
     *
     * @param url
     * @return
     */


    public static String getServieName(String url) {
        if (TextUtils.isEmpty(url)) {
            return null;
        }
        // TODO: 2023/10/13 待添加逻辑 
//        if (url.contains(ApiConstants.BASE_IPS_SYSTEM_URL)) {
//            return IPS_SYSTEM_NAME; //"IPS系统";
//        }
//        if (url.contains(ApiConstants.BASE_HDYSS_SYSTEM_URL)) {
//            return HDYSS_SYSTEM_NAME;//"互动云搜索";
//        }
//        if (url.contains(ApiConstants.BASE_ORDER_MANAGE_SYSTEM_URL)) {
//            return ORDER_MANAGE_SYSTEM_NAME; //"订单系统";
//        }
//        if (url.contains(ApiConstants.BASE_BUS_DATA_MANAGE_SYSTEM_URL)) {
//            return BUS_DATA_MANAGE_SYSTEM_NAME; //"业务数据管理系统";
//        }
//        if (url.contains(ApiConstants.BASE_I_ORDER_SYSTEM_URL)) {
//            return I_ORDER_SYSTEM_NAME;//"订购网关";
//        }
//        if (url.contains(ApiConstants.BASE_IAD_SYSTEM_URL)) {
//            return IAD_SYSTEM_NAME;//"广告系统";
//        }
//        if (url.contains(ApiConstants.BASE_IPEPG_SYSTEM_URL)) {
//            return IPEPG_SYSTEM_NAME;//"ipepg系统";
//        }
//        if (url.contains(ApiConstants.BASE_IPEPG_SYSTEM_URL2)) {
//            return IPEPG2_SYSTEM_NAME;//"IP直播管理系统";
//        }
//        if (url.contains(ApiConstants.BASE_RECOMMEND_SYSTEM_URL)
//                || url.contains(ApiConstants.BASE_RECOMMEND_SYSRANK_URL)) {
//            return RECOMMEND_SYSTEM_NAME;//"推荐系统";
//        }
//        if (url.contains(ApiConstants.BASE_USER_CENTER_SYSTEM_URL)) {
//            return USER_CENTER_SYSTEM_NAME;//"用户中心系统";
//        }
        return "unkown";
    }

    public static class Edata {
        String errorCode;
        String errorReason;
    }

    public static Edata get_edata(String posturl, String urlbody) {
        String serviceName = getServieName(posturl);
        switch (serviceName) {
            case IPS_SYSTEM_NAME:
                return get_edata_ips(posturl, urlbody);
            case HDYSS_SYSTEM_NAME:
                return get_edata_hdyss(posturl, urlbody);
            case ORDER_MANAGE_SYSTEM_NAME:
                return get_edata_ordermanage(posturl, urlbody);
            case BUS_DATA_MANAGE_SYSTEM_NAME:
                return get_edata_busdatamanage(posturl, urlbody);
            case I_ORDER_SYSTEM_NAME:
                return get_edata_iorder(posturl, urlbody);
            case IAD_SYSTEM_NAME:
                return get_edata_iad(posturl, urlbody);
            case IPEPG_SYSTEM_NAME:
                return get_edata_ipepg(posturl, urlbody);
            case IPEPG2_SYSTEM_NAME:
                return get_edata_ipepg2(posturl, urlbody);
            case RECOMMEND_SYSTEM_NAME:
                return get_edata_irs(posturl, urlbody);
            case USER_CENTER_SYSTEM_NAME:
                return get_edata_usercenter(posturl, urlbody);
        }
        //默认返回
        Edata edata = new Edata();
        edata.errorCode = "";
        edata.errorReason = "";
        return edata;
    }

    private static Edata get_edata_ipepg2(String posturl, String urlbody) {
        Edata edata = new Edata();
        edata.errorCode = "";
        edata.errorReason = "";
        if (posturl.contains("getLiveList")) {
            if (urlbody.contains("WASU_LIVE_CHANNEL")) {
                edata.errorCode = "N51001";
                edata.errorReason = "频道组及频道列表查询接口异常";
            } else if (urlbody.contains("WASU_LIVE_EVENT")) {
                edata.errorCode = "N51002";
                edata.errorReason = "频道节目单列表查询接口异常";
            } else if (urlbody.contains("WASU_LIVE_CHANNEL_DTL")) {
                edata.errorCode = "N51003";
                edata.errorReason = "频道详细信息查询接口异常";
            }
        }
        return edata;
    }

    private static Edata get_edata_ipepg(String posturl, String urlbody) {
        Edata edata = new Edata();
        edata.errorCode = "";
        edata.errorReason = "";
        if (posturl.contains("interface")) {
            if (urlbody.contains("WASU_LIVE_CHANNEL")) {
                edata.errorCode = "N50001";
                edata.errorReason = "获取分组及频道信息接口异常";
            } else if (urlbody.contains("WASU_LIVE_EVENT")) {
                edata.errorCode = "N50002";
                edata.errorReason = "获取节目单接口异常";
            }
        }
        return edata;
    }

    private static Edata get_edata_iad(String posturl, String urlbody) {
        Edata edata = new Edata();
        edata.errorCode = "";
        edata.errorReason = "";
        if (posturl.contains("iads_ol/picad")) {
            edata.errorCode = "H50001";
            edata.errorReason = "请求图文广告列表接口异常";
        } else if (posturl.contains("iads_ol/ad")) {
            edata.errorCode = "H50002";
            edata.errorReason = "请求视频广告列表接口异常";
        }
        return edata;
    }

    private static Edata get_edata_iorder(String posturl, String urlbody) {
        Edata edata = new Edata();
        edata.errorCode = "";
        edata.errorReason = "";
        if (posturl.contains("api2/portalRequest")) {
            edata.errorCode = "G50001";
            edata.errorReason = "查询订购网关的相关数据接口异常";
        } else if (posturl.contains("api2/ossrequest")) {
            edata.errorCode = "G50002";
            edata.errorReason = "查询在线结单相关数据接口异常";
        } else if (posturl.contains("api2/getAAAuserinfo")) {
            edata.errorCode = "G50003";
            edata.errorReason = "查询新3A用户信息接口异常";
        } else if (posturl.contains("api2/getAAAqueryorders")) {
            edata.errorCode = "G50004";
            edata.errorReason = "用户信息订购查询接口异常";
        } else if (posturl.contains("api2/contentAuth")) {
            edata.errorCode = "G50005";
            edata.errorReason = "新3A内容鉴权接口异常";
        }
        return edata;
    }

    private static Edata get_edata_busdatamanage(String posturl, String urlbody) {
        Edata edata = new Edata();
        edata.errorCode = "";
        edata.errorReason = "";
        if (posturl.contains("community/getUserGroups")) {
            edata.errorCode = "E50003";
            edata.errorReason = "用户分组信息查询接口异常";
        }
        return edata;
    }

    private static Edata get_edata_ordermanage(String posturl, String urlbody) {
        Edata edata = new Edata();
        edata.errorCode = "";
        edata.errorReason = "";
        return edata;
    }

    private static Edata get_edata_hdyss(String posturl, String urlbody) {
        Edata edata = new Edata();
        edata.errorCode = "";
        edata.errorReason = "";
        if (posturl.contains("isearch_ol/search/contentSearch")) {
            edata.errorCode = "C50001";
            edata.errorReason = "融合搜索接口异常";
        } else if (posturl.contains("isearch_ol/search/query")) {
            edata.errorCode = "C50002";
            edata.errorReason = "联想词/热词搜索接口异常";
        } else if (posturl.contains("isearch_ol/search/hotSearchList")) {
            edata.errorCode = "C50003";
            edata.errorReason = "日搜排行榜接口异常";
        } else if (posturl.contains("isearch_ol/search/fusionSearch")) {
            edata.errorCode = "C50004";
            edata.errorReason = "新融合搜索接口异常";
        }
        return edata;
    }

    /**
     * 推荐系统
     *
     * @param posturl
     * @param urlbody
     * @return
     */
    private static Edata get_edata_irs(String posturl, String urlbody) {

        Edata edata = new Edata();
        edata.errorCode = "";
        edata.errorReason = "";
        if (posturl.contains("irs_ol/getirs")) {
            edata.errorCode = "B50001";
            edata.errorReason = "请求推荐信息接口异常";
        } else if (posturl.contains("irs_rank/rank")) {
            edata.errorCode = "B50002";
            edata.errorReason = "排行榜数据服务接口异常";
        } else if (posturl.contains("irs_ol/special")) {
            edata.errorCode = "B50004";
            edata.errorReason = "专题信息查询接口异常";
        } else if (posturl.contains("irs_ol/live2vodOptional")) {
            edata.errorCode = "B50007";
            edata.errorReason = "带选项直播推荐点播内容接口异常";
        } else if (posturl.contains("irs_ol/mightlike")) {
            edata.errorCode = "B50008";
            edata.errorReason = "猜你喜欢接口异常";
        } else if (posturl.contains("irs_rank/featuredrank")) {
            edata.errorCode = "B50009";
            edata.errorReason = "精选榜单接口异常";
        }
        return edata;
    }

    /**
     * IPS系统
     * 新融合门户
     *
     * @param posturl
     * @param urlbody
     * @return
     */
    private static Edata get_edata_ips(String posturl, String urlbody) {
        Edata edata = new Edata();
        edata.errorCode = "";
        edata.errorReason = "";
        if (posturl.contains("/dataquery/movieDetail")) {
            edata.errorCode = "A50001";
            edata.errorReason = "电影内容详细查询接口异常";
        } else if (posturl.contains("/dataquery/seriesDetail")) {
            edata.errorCode = "A50002";
            edata.errorReason = "电视剧内容详细查询接口异常";
        } else if (posturl.contains("/dataquery/columnDetail")) {
            edata.errorCode = "A50003";
            edata.errorReason = "视频新闻和视频栏目内容详细查询接口异常";
        } else if (posturl.contains("/dataquery/segments")) {
            edata.errorCode = "A50004";
            edata.errorReason = "拆条查询接口异常";
        } else if (posturl.contains("/dataquery/addFolderFavorites")) {
            edata.errorCode = "A50005";
            edata.errorReason = "栏目收藏添加接口异常";
        } else if (posturl.contains("/dataquery/delFolderFavorites")) {
            edata.errorCode = "A50006";
            edata.errorReason = "栏目收藏删除接口异常";
        } else if (posturl.contains("/dataquery/queryFolderFavorites")) {
            edata.errorCode = "A50007";
            edata.errorReason = "栏目收藏查询接口异常";
        } else if (posturl.contains("/dataquery/queryFolderRecentlyPlay")) {
            edata.errorCode = "A50008";
            edata.errorReason = "栏目最近播放查询接口异常";
        } else if (posturl.contains("/dataquery/addPersonalSet")) {
            edata.errorCode = "A50009";
            edata.errorReason = "用户个性化数据添加接口异常";
        } else if (posturl.contains("/dataquery/queryPersonalSet")) {
            edata.errorCode = "A50010";
            edata.errorReason = "用户个性化数据查询接口异常";
        } else if (posturl.contains("/dataquery/delPersonalSet")) {
            edata.errorCode = "A50011";
            edata.errorReason = "用户个性化删除接口异常";
        } else if (posturl.contains("/dataquery/queryUserAllFavorites")) {
            edata.errorCode = "A50012";
            edata.errorReason = "用户全部收藏查询接口异常";
        } else if (posturl.contains("/dataquery/queryAssetIdByContentId")) {
            edata.errorCode = "A50013";
            edata.errorReason = "内容AssetId查询接口异常";
        } else if (posturl.contains("/dataquery/specialDetail")) {
            edata.errorCode = "A50014";
            edata.errorReason = "专题详细查询接口异常";
        } else if (posturl.contains("/dataquery/querySpecialRecentlyPlay")) {
            edata.errorCode = "A50015";
            edata.errorReason = "专题最近播放查询接口异常";
        } else if (posturl.contains("/dataquery/folderContents")) {
            edata.errorCode = "A50017";
            edata.errorReason = "栏目下内容查询接口异常";
        } else if (posturl.contains("/dataquery/folder")) {
            edata.errorCode = "A50016";
            edata.errorReason = "栏目查询接口异常";
        } else if (posturl.contains("/dataquery/childFolders")) {
            edata.errorCode = "A50018";
            edata.errorReason = "子栏目查询接口异常";
        }
        return edata;
    }

    /**
     * 用户中心系统
     *
     * @param posturl
     * @param urlbody
     * @return
     */
    private static Edata get_edata_usercenter(String posturl, String urlbody) {
        Edata edata = new Edata();
        edata.errorCode = "";
        edata.errorReason = "";
        return edata;
    }
}
